<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/2
 * Time: 13:17
 */

namespace Lib\Api;


class UserApi extends UtilsApi {
    private static $modelName = 'User';

    /** 获取用户列表
     * @param $where
     * @param $left
     * @param int $page_num
     * @param int $page_size
     * @param string $field
     * @param string $order
     * @return mixed
     */
    public static function getList($where, $left="", $page_num = 1, $page_size = 15, $field = "*", $order='') {
        $model = M();
        $list = $model->table('fs_user a')
            ->join($left)
            ->field($field)
            ->where($where)
            ->order($order)
            ->group('a.id')
            ->page($page_num, $page_size)
            ->select();

        return $list;
    }

    /** 获取用户总数
     * @param $where
     * @param $left
     * @return mixed
     */
    public static function getTotal($where, $left="") {
        $model = M(UserApi::$modelName);
        $count = $model->table('fs_user a')
            ->join($left)
            ->where($where)
            ->count('a.id');

        return $count;
    }


    /** 获取用户总数
     * @param $where
     * @param $field
     * @param $left
     * @return mixed
     */
    public static function getSum($where, $field, $left="") {
        $model = M(UserApi::$modelName);
        $count = $model->table('fs_user a')
            ->join($left)
            ->where($where)
            ->sum($field);

        return $count;
    }

    public static function getListAllByCondition($where, $left='', $field= "*", $order='', $group='') {
        $model = M();
        $list = $model->table('fs_user a')
            ->join($left)
            ->field($field)
            ->where($where)
            ->order($order)
            ->group($group)
            ->select();

        return $list;
    }

    /** 获取用户总数
     * @param $where
     * @param $left
     * @return mixed
     */
    public static function getTotalByCondition($where, $left='') {
        $model = M();
        $list = $model->table('fs_user a')
            ->join($left)
            ->where($where)
            ->count('a.id');

        return $list;
    }

    /** 获取用户信息
     * @param $where
     * @param $field
     * @return mixed
     */
    public static function getInfo($where, $field= "*") {
        $user = parent::getInfo(UserApi::$modelName, $where, $field);
        return $user;
    }

    /** 根据条件获取用户信息
     * @param $where
     * @param $left
     * @param string $field
     * @return mixed
     */
    public static function getInfoByCondition($where, $left='', $field= "*") {
        $model = M();
        $user = $model->table('fs_user a')
            ->join($left)
            ->field($field)
            ->where($where)
            ->find();

        return $user;
    }

    /** 添加用户
     * @param $data
     * @return mixed
     */
    public static function add($data) {
        $id = parent::add(UserApi::$modelName, $data);
        return $id;
    }

    /** 修改用户
     * @param $data
     * @return bool
     */
    public static function save($data) {
        $state = parent::save(UserApi::$modelName, $data);
        return $state;
    }

    /** 保存数据
     * @param $where
     * @param $data
     * @return bool
     */
    public static function saveByCondition($where, $data) {
        $model = M(UserApi::$modelName);
        return $model->where($where)->save($data);
    }

    /** 删除用户
     * @param $where
     */
    public static function delete($where) {
        parent::delete(UserApi::$modelName, $where);
    }

    /** 生成邀请码
     * @param $len
     * @return string
     */
    public static function generate_invite_code($len) {
        $strs = rand_number(1, $len);
        if (UserApi::check_code($strs)) {
            $result = UserApi::getTotal(array('code' => $strs));
            if ($result == 0) {
                return $strs;
            } else {
                return rand_number(1, $len);
            }
        } else {
            return rand_number(1, $len);
        }
    }

    /** 验证邀请码
     * @param $str
     * @return bool
     */
    private static function check_code($str){
        $isok = true;
        $z = 5;
        for($i=0;$i<=5;$i++){
            $five="";
            $x = 0;
            for ($j=$z;$j>0;$j--) {
                $five = $five.$str{$i+$x};
                $x++;
            }
            $z--;
            $now_chong = $str{$i}.$str{$i}.$str{$i}.$str{$i}.$str{$i};
            $now_zeng = $str{$i}.chr(ord($str{$i})+1).chr(ord($str{$i})+2).chr(ord($str{$i})+3).chr(ord($str{$i})+4);
            if($five==$now_chong){
                $isok = false;
                break;
            }elseif($five==$now_zeng){
                $isok = false;
                break;
            }
        }
        if($isok){
            return true;
        }else{
            return false;
        }
    }
}